<template>
  <Category :scene="scene"></Category>
  <el-card class="box-card">
    <div v-show="scene === 0">
      <el-button
        type="primary"
        @click="addAttrFlag"
        icon="Plus"
        :disabled="cateStore.c3Id ? false : true"
        style="margin-bottom: 15px"
      >
        添加属性
      </el-button>
      <el-table style="width: 100%" border :data="tableData">
        <el-table-column
          label="序号"
          width="70px"
          align="center"
          type="index"
        ></el-table-column>
        <el-table-column
          prop="attrName"
          label="属性名称"
          width="120px"
        ></el-table-column>
        <el-table-column label="属性值名称">
          <template #="{ row, $index }">
            <el-tag
              style="margin-right: 10px"
              type="success"
              v-for="(item, index) in row.attrValueList"
              :key="item.id"
            >
              {{ item.valueName }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="140px">
          <template #="{ row, $index }">
            <el-button
              type="primary"
              size="small"
              icon="Edit"
              @click="scene = 1"
            ></el-button>
            <el-button type="primary" size="small" icon="Delete"></el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div v-show="scene === 1">
      <el-form :inline="true">
        <el-form-item label="属性名称">
          <!-- 宽度如何跟随变化 -->
          <el-input
            v-model="attrObj.attrName"
            placeholder="请输入属性名称"
          ></el-input>
        </el-form-item>
      </el-form>
      <el-button
        @click="addAttrValue"
        type="primary"
        icon="Plus"
        :disabled="attrObj.attrName ? false : true"
      >
        添加属性值
      </el-button>
      <el-button type="primary" @click="cancel">取消</el-button>
      <el-table :data="attrObj.attrValueList" border style="margin: 12px 0">
        <el-table-column
          header-align="center"
          label="序号"
          type="index"
          width="80px;"
        ></el-table-column>
        <el-table-column label="属性值名称">
          <template #="{ row, $index }">
            <!-- 失焦事件：显示 div  获取焦点：显示 input -->
            <!-- ref 新用法  获取 input 实例新用法 -->
            <el-input
              :ref="(vc: any) => (inputArr[$index] = vc)"
              size="small"
              v-if="row.flag"
              @change="loseFocus(row, $index)"
              v-model="row.valueName"
              placeholder="请输入属性值名称"
            ></el-input>
            <div v-else @click="getFocus(row)">{{ row.valueName }}</div>
          </template>
        </el-table-column>
        <el-table-column label="属性值操作" width="180px;">
          <template #="{ row, $index }">
            <el-button
              type="primary"
              size="small"
              icon="Edit"
              @click="editValue(row, $index)"
            ></el-button>
            <el-button
              type="primary"
              size="small"
              icon="Delete"
              @click="deletValue($index)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-button type="primary" @click="save">保存</el-button>
      <el-button type="primary" @click="cancel">取消</el-button>
    </div>
  </el-card>
</template>

<script setup lang="ts">
import { nextTick, reactive, ref, watch } from 'vue'
import Category from '../../../components/Category/index.vue'
import useCategoryStore from '../../../store/modules/category'
import { reqAttrs, reqOperateAttrs } from '../../../api/product/attr'
import {
  AttrObj,
  AttrResponse,
  AttrValueObj,
} from '../../../api/product/attr/type'
import { ElMessage } from 'element-plus'
let cateStore = useCategoryStore()
let tableData = reactive<any[]>([])
let scene = ref<number>(0)
let inputArr = reactive<any[]>([])
let attrObj = reactive<AttrObj>({
  attrName: '',
  attrValueList: [],
  categoryId: '',
  categoryLevel: 3,
})

watch(
  () => cateStore.c3Id,
  async () => {
    if (cateStore.c3Id) {
      let res: AttrResponse = await reqAttrs(
        cateStore.c1Id,
        cateStore.c2Id,
        cateStore.c3Id,
      )
      if (res.code === 200) {
        tableData = res.data
      }
    }
  },
)
const cancel = () => {
  scene.value = 0
}

const addAttrFlag = () => {
  Object.assign(attrObj, {
    attrName: '',
    attrValueList: [],
    categoryId: cateStore.c3Id,
    categoryLevel: 3,
  })

  scene.value = 1
}

const addAttrValue = () => {
  attrObj.attrValueList.push({
    valueName: '',
    flag: true,
  })
  nextTick(() => {
    inputArr[attrObj.attrValueList.length - 1].focus()
  })
}

const loseFocus = (row: AttrValueObj, index: number) => {
  // 判断没有值
  if (row.valueName.trim().length === 0) {
    attrObj.attrValueList.splice(index, 1)
    ElMessage({
      type: 'error',
      message: '属性值不能为空',
    })
    return
  }
  // 判断重复值
  let repeat = attrObj.attrValueList.find((item) => {
    if (item !== row) {
      return item.valueName === row.valueName
    }
  })
  if (repeat) {
    attrObj.attrValueList.splice(index, 1)
    ElMessage({
      type: 'error',
      message: '属性值不能重复',
    })
    return
  }
  row.flag = false
}

const getFocus = (row: AttrValueObj) => {
  row.flag = true
}

const deletValue = ($index: number) => {
  attrObj.attrValueList.splice($index, 1)
}

const editValue = (row: AttrValueObj, index: number) => {
  row.flag = true
  inputArr[index].focus()
}

const save = async () => {
  let res: any = await reqOperateAttrs(attrObj)
  console.log(12344567, res)
  if (res.code === 200) {
    scene.value = 0
    ElMessage({
      type: 'success',
      message: attrObj.id ? '修改成功' : '添加成功',
    })
  } else {
    ElMessage({
      type: 'error',
      message: attrObj.id ? '修改失败' : '添加失败',
    })
  }
}
</script>

<style lang="scss"></style>
